from django.shortcuts import render
from rest_framework.views import APIView
from rest_framework.response import Response
from .models import *
from .ser import *

from rest_framework.generics import GenericAPIView  # 1. 主类--数据

# 2. 混入类---方法
from rest_framework.mixins import CreateModelMixin  # 有create方法--能够新增
from rest_framework.mixins import DestroyModelMixin  # 有destroy方法--能够删除
from rest_framework.mixins import UpdateModelMixin  # 有update方法--能够修改
from rest_framework.mixins import ListModelMixin  # 有list方法--能够查所有
from rest_framework.mixins import RetrieveModelMixin  # 有retrieve方法--能够查单个

# 导入版本03--需要继承的模块
from rest_framework.generics import CreateAPIView
from rest_framework.generics import ListAPIView
from rest_framework.generics import UpdateAPIView
from rest_framework.generics import DestroyAPIView
from rest_framework.generics import RetrieveAPIView

from rest_framework.generics import ListCreateAPIView, RetrieveUpdateDestroyAPIView

from rest_framework.views import APIView
from rest_framework.response import Response


# # 1.1 商品接口----版本001
# class GoodsView(APIView):
#     # 1.1 新增商品
#     def post(self, request):
#         # 1. 获取数据
#         # print(request.data, '---------------001')
#         # 2. 反序列化
#         goods_ser = GoodsSer(data=request.data)
#         # 3. 验证
#         goods_ser.is_valid(raise_exception=True)
#         # 4. 保存
#         goods_ser.save()
#         # 5. 响应
#         return Response({'msg': '新增商品成功', 'code': 200})


from rest_framework.generics import GenericAPIView
from rest_framework.mixins import CreateModelMixin


# # 1.2 新增和查所有商品接口---版本002
# class GoodsView(GenericAPIView, CreateModelMixin, ListModelMixin):
#
#     # 查询集
#     queryset = Goods.objects.all()
#     # 序列化器
#     serializer_class = GoodsSer
#
#     # 新增方法
#     def post(self, request, *args, **kwargs):
#         return self.create(request, *args, **kwargs)
#
#     # 查所有
#     def get(self, request, *args, **kwargs):
#         return self.list(request, *args, **kwargs)


# # 1.2 查单个/删除/修改---版本002
# class GoodsSoloView(GenericAPIView, RetrieveModelMixin, DestroyModelMixin, UpdateModelMixin):
#     # 查询集
#     queryset = Goods.objects.all()
#     # 序列化器
#     serializer_class = GoodsSer
#
#     # 查单个
#     def get(self, request, *args, **kwargs):
#         return self.retrieve(request, *args, **kwargs)
#
#     # 删除
#     def delete(self, request, *args, **kwargs):
#         return self.destroy(request, *args, **kwargs)
#
#     # 修改--全部修改
#     def put(self, request, *args, **kwargs):
#         return self.update(request, *args, **kwargs)
#
#     # 修改--部分修改
#     def patch(self, request, *args, **kwargs):
#         return self.partial_update(request, *args, **kwargs)


# from rest_framework.generics import CreateAPIView  # 新增
# from rest_framework.generics import ListAPIView  # 查所有
#
# from rest_framework.generics import UpdateAPIView  # 修改
# from rest_framework.generics import DestroyAPIView  # 删除
# from rest_framework.generics import RetrieveAPIView  # 查单个
#
#
# # 1.3 新增和查所有商品接口---版本003
# class GoodsView(CreateAPIView, ListAPIView):
#     # 查询集
#     queryset = Goods.objects.all()
#     # 序列化器
#     serializer_class = GoodsSer
#
#
# # 1.3 查单个/删除/修改---版本003
# class GoodsSoloView(RetrieveAPIView, DestroyAPIView, UpdateAPIView):
#     # 查询集
#     queryset = Goods.objects.all()
#     # 序列化器
#     serializer_class = GoodsSer


# from rest_framework.generics import ListCreateAPIView
# from rest_framework.generics import RetrieveUpdateDestroyAPIView

# 业务接口---ApiView--加验证码，发邮件，手机短信
# 数据接口---单纯增删改查


# # 1.4 新增和查所有商品接口---版本004
# class GoodsView(ListCreateAPIView):
#     # 查询集
#     queryset = Goods.objects.all()
#     # 序列化器
#     serializer_class = GoodsSer
#
#
# # 1.4 查单个/删除/修改---版本004
# class GoodsSoloView(RetrieveUpdateDestroyAPIView):
#     # 查询集
#     queryset = Goods.objects.all()
#     # 序列化器
#     serializer_class = GoodsSer


# from rest_framework.viewsets import ModelViewSet
#
#
# # 1.5 新增、查所有、查单个、删除、修改--版本5--终极版本
# class GoodsViewset(ModelViewSet):
#
#     # 查询集
#     queryset = Goods.objects.all()
#     # 序列化器
#     serializer_class = GoodsSer


# 1. 首页接口
class IndexView(APIView):

    def get(self, request):
        # 1. 查到所有分类
        cate_li = Cate.objects.all()

        data_li = []
        # 2. 遍历分类，截取4个最新的商品和3个最畅销的商品
        for cate in cate_li:

            # 最新的4个商品
            new_goods = cate.goods_set.all().order_by('-create_time')[:4].values()

            # 最畅销的3个商品
            hot_goods = cate.goods_set.all().order_by('-sell_num')[:3].values()

            cate_dic = {
                'id': cate.id,
                'cate': cate.name,
                'new_goods': new_goods,
                'hot_goods': hot_goods
            }

            # 把每个分类下组装好的数据，放到列表中
            data_li.append(cate_dic)

        # 响应
        return Response({'cate_li': data_li})


# 2. 商品列表页
class GoodsListView(APIView):

    def get(self, request):

        # 1. 获取分类cate_id 和 页码page
        cate_id = request.GET.get('cate_id')
        page = int(request.GET.get('page', 1))
        size = 10

        # 2. 计算start和end
        start = (page - 1)*size
        end = page*size

        # # 3. 查询数据，并序列化
        goods_li = Goods.objects.filter(cate_id=cate_id)[start:end].values()
        total = Goods.objects.filter(cate_id=cate_id).count()

        # 4. 返回
        res = {
            'goods_li': goods_li,  # 当前页数据
            'total': total   # 当前分类下，商品总数
        }
        return Response(res)


# from rest_framework.generics import ListAPIView, ListCreateAPIView
#
#
# class GoodsListView(ListAPIView):
#
#     queryset = Goods.objects.all()
#     serializer_class = GoodsSer

    # def list(self, request, *args, **kwargs):
    #
    #     cate_id = request.GET.get('cate_id')
    #     print(cate_id, '-------------888888888888888888')
    #
    #     queryset = Goods.objects.filter(cate_id=cate_id)
    #
    #     page = self.paginate_queryset(queryset)
    #     if page is not None:
    #         serializer = self.get_serializer(page, many=True)
    #         return self.get_paginated_response(serializer.data)
    #
    #     serializer = self.get_serializer(queryset, many=True)
    #     return Response(serializer.data)


